package com.comment.project.tomcat;



import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class GetConnetion1 {
    Connection connection = null;
    public static void main(String[] args) {
        GetConnetion1 connetion1 = new GetConnetion1();
        connetion1.getConnection2();
    }

    // 获取数据库连接
    public  Connection getConnection2() {
        try {
            //初始化上下文
            Context cxt;
            cxt = new InitialContext();
            //获取与逻辑名相关联的数据源对象
            DataSource ds=(DataSource)cxt.lookup("java:comp/env/jdbc/news");
            connection=ds.getConnection();
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
}
